﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using WMS_MAV.Aubit;
using WMS_MAV.DTO.Carrier;

namespace WMS_MAV.Controllers
{
    public class CarrierController : Controller
    {
        private readonly IConfiguration _configuration;
        public CarrierController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }
        /// <summary> 
        /// 承运商列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CarrierShow()
        {
            return View();
        }

        /// <summary>
        /// 返回承运商数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetCarrierShow(CarrierShowDto dto)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "carrierRead/api/Carrier/GetAllCarrier?pageIndex=" + dto.PageIndex + "&pageSize=" + dto.PageSize + "&carrierNo=" + dto.CarrierNo + "&carrierName=" + dto.CarrierName;
            var response = await client.GetAsync(url);
            return Json(response);
        }
        
       /// <summary>
       /// 添加承运商
       /// </summary>
       /// <param name="data"></param>
       /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetCarrierAdd(CarrierAddDto dto)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "carrier/api/Carrier/AddCarrier";
            var response = await client.PostAsync(url, dto.Data);
            return Json(response);
        }
        /// <summary>
        /// 删除承运商
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetCarrierDel(CarrierDeleteDto dto)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "carrier/api/Carrier/DeleteCarrier";
            var response = await client.PostAsync(url, dto.Data);
            return Json(response);
        }
        /// <summary>
        /// 批量删除承运商
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetCarrierDelRange(CarrierRangeDeleteDto dto)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "carrier/api/Carrier/DeleteRangeCarrier";
            var list = "{\"ids\":"+ dto.Ids + "}";
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 反填承运商数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetCarrierModel(CarrierByIdDto dto)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "carrierRead/api/Carrier/GetModelCarrier?CarrierId=" + dto.Id;
            var response = await client.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 修改承运商
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetCarrierUpd(CarrierUpdDto obj1,string token)
        {
            
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "carrier/api/Carrier/UpdateCarrier";
            string list = JsonConvert.SerializeObject(obj1);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 导出承运商数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetCarrierExcel(CarrierExcelDto dto)
        {
           
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "carrierRead/api/Carrier/ExportExcel?pageIndex=" + dto.PageIndex + "&pageSize=" + dto.PgaeSize + "&carrierNo=" + dto.CarrierNo + "&carrierName=" + dto.CarrierName;
            var response = await client.GetAsync(url);
            return Json(response);
        }
    }
}
